package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author moningxi
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> dbCartList = shoppingCartMapper.queryByUserAndDishInfo(shoppingCart);
        if (dbCartList.isEmpty()) {
            // 不存在 新增
            if (dto.getDishId() != null) {
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else if (dto.getSetmealId() != null) {
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.add(shoppingCart);
        } else {
            // 存在增加数量
            ShoppingCart dbCart = dbCartList.get(0);
            dbCart.setNumber(dbCart.getNumber() + 1);
            shoppingCartMapper.update(dbCart);
        }
    }

    /**
     * 查看当前用户的购物车数据
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> carts = shoppingCartMapper.queryByUserAndDishInfo(shoppingCart);
        return carts;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    /**
     * 删除购物车中一个商品
     */
    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        List<ShoppingCart> carts = shoppingCartMapper.queryByUserAndDishInfo(shoppingCart);
        if (!carts.isEmpty()) {
            ShoppingCart dbCart = carts.get(0);
            int num = dbCart.getNumber() - 1;
            if (num == 0) {
                // 删除这条数据
                shoppingCartMapper.deleteById(dbCart.getId());
            } else {
                dbCart.setNumber(num);
                shoppingCartMapper.update(dbCart);
            }
        }
    }
}
